$(function(){
     //设置当前城市
    var currentCity;
    function setAddress(){
        var addr=Utils.getCookie("currentCity");
        console.log(addr+"============================addr");
        if(addr){
            currentCity=addr;
            $("#currentCity").html(addr);
             $(".changecity span").html(addr);
        }else{
            currentCity=Utils.getCurrentCity("currentCity");
            $("#currentCity").html(currentCity); 
            $(".changecity span").html(currentCity);
        }
    }
    setAddress();
    //未登录跳入登录页面
    var hasLogin=Utils.getCookie("hasLogin");
    if(!hasLogin){
        location.href="./login.html";
     }
    //跳转进入选择地址
    $(".changecity a").on("click",function(){
        location.href="./address.html?currentCity="+currentCity;
    });
    
    function jump_page(ele){
        var ele=ele;
        $(ele).on("click",function(){
            var links=$(this).attr("data-links");
            location.href="./"+links+".html?currentCity="+currentCity;
        });   
    }
    jump_page(".jump_links");
    
    $(".nav_down").on("click",function(){
        if($(".choose-bar").css("display")=="none"){
            $(".choose-bar").css("display","block");
            $(".classifynav .nav_down i").addClass("bgup").removeClass("bgdown");
        }else{
            $(".choose-bar").css("display","none");
            $(".classifynav .nav_down i").addClass("bgdown").removeClass("bgup");
        }
    });

    $(".zwl-first").on("click",function(){
        location.href="./myService.html?currentCity="+currentCity;
    });
    $(".zwl-second").on("click",function(){
        location.href="./myShopping.html?currentCity="+currentCity;
    });
    //数据展示
    var currentPage=1;//默认设置当前页为第一页
        var totalPage=1;//设置默认总页数
        var show_count=15;//设置默认每次加载的条数
        var resType=1;//默认类型
        var url=Utils.reqUrl,//请求url
        async=true,
        type="post";
        var productName="";
        //获取我的服务数据的接口
        var getMyShopList=JSON.stringify({
            "code":"getProductList",
            "version":"1.0",
            "data":{
                "areaId":"",
                "parentPdTypeId":0,
                "userId":0,
                "address":currentCity,
                "currentPage":currentPage,
                "showCount":show_count,
                "productName":productName,
                "productTypeId":0
            }
        });
        
        getData(url,getMyShopList,call_func_shoptotalpage,async,type);
    //ajax获取数据
        function getData(url,datas,call_func,async,type){
            console.log("请求参数:"+datas);
            $.ajax({
                url:url,
                type:type,
                datatype:"json",
                async:async,
                contentType:"application/json",
                data:datas,
                success:function(data){
                    call_func(data);
                },
                error:function(msg){
                    console.log(msg);
                }         
            });        
        }
    
    //获取服务总页数
        function call_func_shoptotalpage(data){
            console.log(data);
            totalPage=data.data.totalPage;
             page();
        }
        function call_func_shop(data){
            console.log(data);
            var p_datas=data.data.datas;//商品数据列
            var p_len=p_datas.length;//商品数据长度
            var p_str='';
            var arr1=[];//未开通增值服务
            var arr2=[];//已开通增值服务
            var dataStr1='';
            var dataStr2='';
            if(p_len>0){
                for(var i=0;i<p_len;i++){
                    if(p_datas[i].sellerFees.length>0){
                            arr2.push(p_datas[i]);    
                    }else{
                        arr1.push(p_datas[i]);
                    }
                }
                if(arr1.length>0){
                    for(var i=0,len=arr1.length;i<len;i++){
                       dataStr1+='<li data-id="'+p_datas[i].productId+'" class="marg_l10">'
                                +'<div class="data_lable_div"></div>'
                                +'<img class="data_img lazy" title="'+p_datas[i].productName+'" src="./images/loading.gif" data-original="'+p_datas[i].mainImgPath+'" >'
                                +'<div class="pro_det"><img src="./images/renzheng.png" ><span class="pro_detail_name">'+p_datas[i].productTypeName+'</span><span class="pro_det_price">￥'+p_datas[i].saleprice+'</span></div>'
                                +'<div class="pro_mark">'+p_datas[i].productName+'</div>'
                                +'<div class="pro_detail">'
                                    +'<p>'+p_datas[i].remark+'</p>'
                                +'</div>'
                                +'<div class="pro_lable">'
                                +'</div>'
                            +'</li>';
                    }
                }
                var downS='';
                if(arr2.length>0){
                    for(var i=0,len=arr2.length;i<len;i++){
                    var tagStr='';
                    var tag_strs='';
                    if(arr2[i].sellerFees){
                        if(arr2[i].sellerFees.length>0){
                            var SellerFree=arr2[i].sellerFees[0].isAvaliable;
                            if(SellerFree=="1"){//已开通增值服务
                                var s_tag=arr2[i].tagServices;
                                var ss_len=s_tag.length;

                                for(var s=0;s<ss_len;s++){
                                    if(s_tag[s].sort=="1"){
                                        tag_strs+='<span class="bgfa6">'+s_tag[s].name+'</span>';    
                                    }else if(s_tag[s].sort=="2"){
                                        tag_strs+='<span class="bg3bc">'+s_tag[s].name+'</span>';
                                    }else if(s_tag[s].sort=="3"){
                                        tag_strs+='<span class="bgfaa">'+s_tag[s].name+'</span>';
                                    }else{
                                        tagStr+='<img src="'+s_tag[s].tagIconPath+'">';
                                    } 
                                }   
                            }       
                        }    
                    }
                    dataStr2+='<li data-id="'+p_datas[i].productId+'" class="marg_l10">'
                                +'<div class="data_lable_div">'+tag_strs+'</div>'
                                +'<img class="data_img lazy" title="'+p_datas[i].productName+'" src="./images/loading.gif" data-original="'+p_datas[i].mainImgPath+'" >'
                                +'<div class="pro_det"><img src="./images/renzheng.png" ><span class="pro_detail_name">'+p_datas[i].productTypeName+'</span><span class="pro_det_price">￥'+p_datas[i].saleprice+'</span></div>'
                                +'<div class="pro_mark">'+p_datas[i].productName+'</div>'
                                +'<div class="pro_detail">'
                                    +'<p>'+p_datas[i].remark+'</p>'
                                +'</div>'
                                +'<div class="pro_lable">'
                                    +tagStr
                                +'</div>'
                            +'</li>';
               }
                }
                $(".productList").html(dataStr2+dataStr1);
                    $("img.lazy").lazyload({
                        threshold : 200
                });
                //点击当前商品，跳转进入商家详情页
                $(".productList li").on("click",function(){
                    var productId=$(this).attr("data-id");
                    location.href="./goodDetail.html?currentCity="+address+"&productId="+productId;
                });
            }
        }
    
    //按照服务名搜索服务
    $(".button").on("click",function(){
        productName=$(".search-bar input").val();
        console.log(productName+"==============");
        page();
    });
    
    //分页操作
    function page(){
        layui.use(['laypage', 'layer'], function(){
              var laypage = layui.laypage,
                  layer = layui.layer;
                  laypage({
                    cont: 'page',
                      pages: totalPage,
                      skip: true,
                      first:false,
                      lase:false,
                      jump: function(obj, first){
                         currentPage = obj.curr;
                         var getpageShopList=JSON.stringify({
                            "code":"getProductList",
                            "version":"1.0",
                            "data":{
                                "areaId":"",
                                "parentPdTypeId":0,
                                "userId":0,
                                "address":currentCity,
                                "currentPage":currentPage,
                                "showCount":show_count,
                                "productName":productName,
                                "productTypeId":0
                            }
                        });
                        getData(url,getpageShopList,call_func_shop,async,type);
                      }
                  });
            });
    }
    
});